**************
To replicate:
**************

Option 1: Run scripts individually

1. Run intial.do to generate .dtas from primary xlsx sources. ctrl-h to find and replace directory
2. Run merge.R to merge election and terror data, output the separate merged datasets as xlsx. Must change directory and install pacman if first use.
3. Run finish.do to label vars and save as .dta

Option 2: Single script, integrating stata with R using RStata.

1. ctrl-h to change directory in initial.do
2. change single working directory in finish.do
3. In total_merge_script.R, change directory and install pacman if first use. Then, change variables "version" and "path" to those of your Stata version, and the .exe location, respectively.
4. Run total_merge_script.R. This script runs initial.do, then merges the data in R, then runs finish.do, all automatically.

*******************
File descriptions:
*******************

In data_do_files:

"total_merge_script.R" - this .R script integrats the stata and R processes, calling stata .do files from within R using the package RStata. For use in option 2 above. 

"intial.do" - This .do loads the presidential, senate, house, and terror data. 1972_to_2016_stata.dta, terror.dta, House_merged_congress.dta, and Senate_merged_congress.dta are saved

"merge.R" - Our main R merge script. Here we merge the terror data with different voting datasets, and export them as .xlsx. Currently contains presidential, senate, and house.

"finish.do" - loads the data output by R and labels vars, saves as .dta in merged_datasets.

"aggregate.R" - aggregates presidential election returns.

In merged_datasets:

"terror_pres_wide.dta" - Presidential election data merged to terror attacks, for before and after each attack. Stata version, with labels.

"terror_house_wide.dta" - House election data merged to terror attacks, for before and after each attack. Stata version, with labels.

"terror_senate_wide.dta" - Senate election data merged to terror attacks, for before and after each attack. Stata version, with labels.

"terror_pres_wide.xlsx" - Presidential election data merged to terror attacks, for before and after each attack. Excel version.

"terror_house_wide.xlsx" - House election data merged to terror attacks, for before and after each attack. Excel version.

"terror_senate_wide.xlsx" - Senate election data merged to terror attacks, for before and after each attack. Excel version.

************************************
Step by Step description of merge.R 
************************************

Note: Same code as total_merge_script.R, but lacks integration with Stata using RStata (thus will not run initial.do and finish.do automatically)

1.	Import presidential data as aggregated from .csv sources in initial.do.

2.	Merge with data on actual election dates (election data only includes year, not day of election)

3.	Using nearDate() from the Survival() package, match every GTD attack with the index (row number in data) of the election we have data on nearest to that attack before it occurred, and after it occurred.

4.	Create index in election data (from row number), and merge on this twice (_t1 and _t2) to create dataset with attack, previous election, and election following attack.

5.	For presidential data only: generate margin of victory variable for republican candidates. Margin already present in congressional data.

6.	Create time variable to record days between election at t1 and attack, and separate variable to record time from attack to election at t2.

7.	Clean and write to .xlsx, for processing in finish.do

8.      Repeat steps 1-4, 6, 7, for house and senate data.

*****************************
Election and Terror Sources:
*****************************

Terror Data (GTD): GTD.xlsx

Motives (manually coded): motives.xlsx

Note: Purchased election data omitted from replication package.

Presidential Election Datasets (David Leips's Election Atlas):

Pres_Election_Data_1972.xls
Pres_Election_Data_1976.xls
Pres_Election_Data_1980.xlsx
Pres_Election_Data_1984.xls
Pres_Election_Data_1988.xls
Pres_Election_Data_1992.xlsx
Pres_Election_Data_1996.xlsx
Pres_Election_Data_2000.xlsx
Pres_Election_Data_2004.xlsx
Pres_Election_Data_2008.xlsx
Pres_Election_Data_2012.xlsx
Pres_Election_Data_2016.xlsx

(aggregated in aggegate.R to presidential.dta, processed in intitial.do to 1972_to_2016.xlsx and 1972_to_2016_stata.dta)

House Datasets (David Leips's Election Atlas): 

	House_Election_Data_1994.csv
	House_Election_Data_1996.csv
	House_Election_Data_1998.csv
	House_Election_Data_2000.csv
	House_Election_Data_2002.csv
	House_Election_Data_2004.csv
	House_Election_Data_2006.csv
	House_Election_Data_2008.csv
	House_Election_Data_2010.csv
	House_Election_Data_2012.csv
	House_Election_Data_2014.csv
	House_Election_Data_2016.csv

(aggregated in initial.do to House_merged_congress.dta)

Senate Datasets (David Leips's Election Atlas):
	
	Sen_Election_Data_1994.csv
	Sen_Election_Data_1996.csv
	Sen_Election_Data_1998.csv
	Sen_Election_Data_2000.csv
	Sen_Election_Data_2002.csv
	Sen_Election_Data_2004.csv
	Sen_Election_Data_2006.csv
	Sen_Election_Data_2008.csv
	Sen_Election_Data_2010.csv
	Sen_Election_Data_2012.csv
	Sen_Election_Data_2014.csv
	Sen_Election_Data_2016.csv

(aggregated in initial.do to Senate_merged_congress.dta)
